#include<iostream>
#include<cmath>
#include<fstream>
#include<string>
using namespace std;

#include"library.h"

double g(double x){
	return x*x - 4*x - 5;
}

void f(double x, double & y, double & dy){
	y = x*x - 4*x -5;
	dy = 2*x - 4;
}

int main(){
	
	double a=-5, b=0, x, y, dy;

	// bisection method
	x = bisection( g, a, b, 1E-9);
	cout<<" bisection: root="<<x<<" y="<< g(x)<<endl;

	a=0; b=6;
	x = bisection( g, a, b, 1E-9);
	cout<<" bisection: root="<<x<<" y="<< g(x)<<endl;

	// Newton's method
	a=-3; b=1;
	x = Newton_findroot( f, a, b, 1E-9);
	f(x, y, dy);
	cout<<" Newton's method: root="<<x<<" y="<<y<<endl;

	a=3; b=7;
	x = Newton_findroot( f, a, b, 1E-9);
	f(x, y, dy);
	cout<<" Newton's method: root="<<x<<" y="<<y<<endl;

	a=-3; b=1;
	x = SecantMethod( g, a, b, 1E-9);
	cout<<" Secant method: root="<<x<<" y="<<g(x)<<endl;

	a=3; b=7;
	x = SecantMethod( g, a, b, 1E-9);
	cout<<" Secant method: root="<<x<<" y="<<g(x)<<endl;

	return 0;
}

/*
 * Results:
 *
 iteration rounds = 33
 bisection: root=-1 y=3.49246e-10

 iteration rounds = 33
 bisection: root=5 y=-6.98492e-10

 iteration rounds = 1
 Newton's method: root=-1 y=0

 iteration rounds = 1
 Newton's method: root=5 y=0

 iteration rounds = 2
 Secant method: root=-1 y=0

 iteration rounds = 2
 Secant method: root=5 y=0
*/
